import 'dart:convert';

import 'package:azlistview_plus/azlistview_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:life_service/model/city.dart';
import 'package:life_service/utils/tools.dart';
import 'package:lpinyin/lpinyin.dart';

class SelectCityController extends GetxController {
  RxList<CityModel> cityList = RxList([]);
  double susItemHeight = 36;

  RxBool isShowSearch = false.obs;

  String imgFavorite = ToolsUtils.getImgPath('ic_favorite');

  FocusNode focusNode = FocusNode();

  TextEditingController textEditingController =
      ToolsUtils.editingController('');

  @override
  void onInit() {
    loadData();
    super.onInit();
  }

  @override
  void onClose() {
    focusNode.dispose();
    textEditingController.dispose();
    super.onClose();
  }

  void loadData() async {
    ///加载城市列表
    rootBundle.loadString('assets/data/china.json').then((value) {
      cityList.clear();
      Map countyMap = json.decode(value);
      List list = countyMap['china'];
      for (var v in list) {
        cityList.add(CityModel.fromJson(v));
      }
      _handleList(cityList);
    });
  }

  void _handleList(List<CityModel> list) {
    for (int i = 0, length = list.length; i < length; i++) {
      String pinyin = PinyinHelper.getPinyinE(list[i].name);
      String tag = pinyin.substring(0, 1).toUpperCase();
      list[i].namePinyin = pinyin;
      if (RegExp('[A-Z]').hasMatch(tag)) {
        list[i].tagIndex = tag;
      } else {
        list[i].tagIndex = '#';
      }
    }
    SuspensionUtil.sortListBySuspensionTag(list);

    SuspensionUtil.setShowSuspensionStatus(cityList);

    cityList.insert(
      0,
      CityModel(name: 'header', tagIndex: imgFavorite),
    );
  }
}

class SelectDateController extends GetxController {
  List<String> weeks = ['一', '二', '三', '四', '五', '六', '日'];
}
